package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.*;
import com.ruoyi.zhgd.domain.vo.CollectPieceDosageVo;
import com.ruoyi.zhgd.mapper.CollectProductMapper;
import com.ruoyi.zhgd.mapper.ZjHntScaleMapper;
import com.ruoyi.zhgd.service.ICollectDosageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.CollectPieceBo;
import com.ruoyi.zhgd.domain.vo.CollectPieceVo;
import com.ruoyi.zhgd.mapper.CollectPieceMapper;
import com.ruoyi.zhgd.service.ICollectPieceService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 盘信息Service业务层处理
 *
 * @author yhy
 * @date 2023-09-20
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class CollectPieceServiceImpl implements ICollectPieceService {

    private final CollectPieceMapper baseMapper;

    @Autowired
    private ICollectDosageService collectDosageService;

    @Autowired
    private CollectProductMapper productMapper;

    @Autowired
    private ZjHntScaleMapper zjHntScaleMapper;

    /**
     * 查询盘信息
     */
    @Override
    public CollectPieceVo queryById(Long pkId){
        return baseMapper.selectVoById(pkId);
    }

    /**
     * 查询盘信息列表
     */
    @Override
    public TableDataInfo<CollectPieceVo> queryPageList(CollectPieceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CollectPiece> lqw = buildQueryWrapper(bo);
        Page<CollectPieceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询盘信息列表
     */
    @Override
    public List<CollectPieceVo> queryList(CollectPieceBo bo) {
        LambdaQueryWrapper<CollectPiece> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CollectPiece> buildQueryWrapper(CollectPieceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CollectPiece> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentPkId() != null, CollectPiece::getParentPkId, bo.getParentPkId());
        lqw.eq(bo.getProduct() != null, CollectPiece::getProduct, bo.getProduct());
        lqw.eq(bo.getDatTim() != null, CollectPiece::getDatTim, bo.getDatTim());
        lqw.eq(StringUtils.isNotBlank(bo.getBlend()), CollectPiece::getBlend, bo.getBlend());
        lqw.eq(bo.getSerial() != null, CollectPiece::getSerial, bo.getSerial());
        lqw.eq(StringUtils.isNotBlank(bo.getBldTim()), CollectPiece::getBldTim, bo.getBldTim());
        lqw.eq(bo.getPieAmnt() != null, CollectPiece::getPieAmnt, bo.getPieAmnt());
        lqw.eq(StringUtils.isNotBlank(bo.getLands()), CollectPiece::getLands, bo.getLands());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), CollectPiece::getFlag, bo.getFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getNote()), CollectPiece::getNote, bo.getNote());
        return lqw;
    }

    /**
     * 新增盘信息
     */
    @Override
    public Boolean insertByBo(CollectPiece bo) {
        CollectPiece add = BeanUtil.toBean(bo, CollectPiece.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPkId(add.getPkId());
        }
        return flag;
    }




    /**
     * 修改盘信息
     */
    @Override
    public Boolean updateByBo(CollectPieceBo bo) {
        CollectPiece update = BeanUtil.toBean(bo, CollectPiece.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CollectPiece entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除盘信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils pieceList(Page page, String pk_id) {

        List<CollectPieceVo> collectPieceVos = baseMapper.queryPage(page, pk_id);
        for (CollectPieceVo collectPieceVo : collectPieceVos){
            List<CollectPieceDosageVo> pieceDosageVos = new ArrayList<>();
            List<CollectDosage> collectDosages = baseMapper.selectListByPkId(collectPieceVo.getPkId());
            for (CollectDosage d:collectDosages) {
                CollectPieceDosageVo pieceDosageVo = new CollectPieceDosageVo();
                pieceDosageVo.setPkId(d.getPkId());
                pieceDosageVo.setMaterial(d.getMaterial());
                pieceDosageVo.setSpec(d.getSpec());
                pieceDosageVo.setFactAmnt(d.getFactAmnt());
                pieceDosageVo.setWatFull(d.getWatFull());
                pieceDosageVo.setRecAmnt(d.getRecAmnt());
                pieceDosageVo.setPlanAmnt(d.getPlanAmnt());
                pieceDosageVo.setScale(d.getPlanAmnt());
                pieceDosageVo.setErrorValue(d.getErrorValue() != null && d.getErrorValue() != 0.0 ? d.getErrorValue() : (d.getFactAmnt() - d.getPlanAmnt()));
                pieceDosageVo.setPercentError(d.getPercentError() != null && d.getPercentError() != 0.0 ? d.getPercentError() : (d.getFactAmnt() - d.getPlanAmnt()) / d.getPlanAmnt() * 100);
                pieceDosageVos.add(pieceDosageVo);
                collectPieceVo.setPieceDosageVos(pieceDosageVos);
            }
        }
        return new PageUtils(page.setRecords(collectPieceVos));
    }
}
